home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / _allocufb.c next >
C/C++ Source or Header  |  1994-03-30  |  1KB  |  64 lines

  1. RCS_ID_C="$Id: _allocufb.c,v 1.2 1994/03/30 07:39:20 jraja Exp $";
  2. /*
  3.  * _allocufb.c --- get a free ufb
  4.  *
  5.  * Author: jraja <Jarno.Rajahalme@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP Network Support Library.
  8.  *
  9.  * Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *                  All rights reserved.
  12.  *
  13.  * Created      : Tue Mar 22 03:57:55 1994 jraja
  14.  * Last modified: Wed Mar 30 10:32:40 1994 jraja
  15.  *
  16.  */
  17.  
  18. #include <ios1.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21.  
  22. /*
  23.  * Allocate new ufb, which is returned as return value. The corresponding fd
  24.  * is returned via fdp.
  25.  */
  26. struct UFB *
  27. __allocufb(int *fdp)
  28. {
  29.   struct UFB *ufb, *last_ufb;
  30.   int         last_fd = 0;
  31.  
  32.   /*
  33.    * find first free ufb
  34.    */
  35.   last_ufb = ufb = __ufbs;
  36.   while (ufb != NULL && ufb->ufbflg != 0) {
  37.     last_ufb = ufb;
  38.     last_fd++;
  39.     ufb = last_ufb->ufbnxt;
  40.   }
  41.   /*
  42.    * Check if need to create one
  43.    */
  44.   if (ufb == NULL) {
  45.     if ((ufb = malloc(sizeof(*ufb))) == NULL) {
  46.       errno = ENOMEM;
  47.       return NULL;
  48.     }
  49.     ufb->ufbnxt = NULL;
  50.     ufb->ufbflg = 0;        /* => unused ufb */
  51.  
  52.     if (last_ufb == NULL)
  53.       __ufbs = ufb;
  54.     else
  55.       last_ufb->ufbnxt = ufb;
  56.     
  57.     *fdp = __nufbs++;
  58.   }
  59.   else
  60.     *fdp = last_fd;
  61.   
  62.   return ufb;
  63. }
  64.